
import datetime
from sqlalchemy.dialects.mysql import JSON

from RealProject.db_init import db,Model,session,BaseModel
from sqlalchemy import *
from app.utils import is_duplicate



class Project(BaseModel):
    __tablename__ = 'project'
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(20))
    description = db.Column(db.String(200),nullable=True)
    create_time = db.Column(db.DateTime, default=datetime.datetime.now)
    update_time = db.Column(db.DateTime, default=datetime.datetime.now, onupdate=datetime.datetime.now)
    is_del = db.Column(db.Integer, default=0)

    def __init__(self, name, description):
        self.name = name
        self.description = description

    def __repr__(self):
        return '<Project %r>' % self.name


class Case(BaseModel):
    __tablename__ = 'case'
    id = db.Column(db.Integer, primary_key=True)
    project_id = db.Column(db.Integer, db.ForeignKey('project.id'))
    name = db.Column(db.String(20))
    description = db.Column(db.String(200),nullable=True)
class RequestsInfo(BaseModel):
    id = db.Column(db.Integer, primary_key=True,)
    title = db.Column(db.String(20), nullable=False,info = {"label":"标题"})
    index = db.Column(db.Integer,nullable=False,info = {"label":'步骤序号'})
    method = db.Column(db.String(10),nullable=False,info = {"label":'请求方式'})
    url =  db.Column(db.String(255),nullable=False,info = {"label":'URL'})
    headers = db.Column(JSON,nullable=True,info = {"label":'请求头'})
    params = db.Column(JSON,nullable=True,info = {"label":'查询参数'})
    payload = db.Column(JSON,nullable=True,info = {"label":'请求体'})
    data  = db.Column(JSON,nullable=True,info = {"label":'请求体'})
    res = db.Column(JSON,nullable=True,info = {"label":'响应体'})
    is_del = db.Column(db.Integer, default=0)
    create_time = db.Column(db.DateTime, default=datetime.datetime.now)
    update_time = db.Column(db.DateTime, default=datetime.datetime.now, onupdate=datetime.datetime.now)

    def __repr__(self):
        return '<RequestsInfo %r>' % self.title

# if __name__ == '__main__':
#     Model.metadata.create_all(engine,extend_existing=True)
class ScriptStep(BaseModel):
    id = db.Column(db.Integer, primary_key=True,)
    scriptName = db.Column(db.String(20), nullable=False,info = {"label":"脚本名称"})
    project_id = db.Column(db.String(20), db.ForeignKey(Project.id),nullable=False,info = {"label":"项目名称"})
    index = db.Column(db.Integer,nullable=False,info = {"label":'步骤序号'})
    method = db.Column(db.String(10),nullable=False,info = {"label":'请求方式'})
    url =  db.Column(db.String(255),nullable=False,info = {"label":'URL'})
    headers = db.Column(JSON,nullable=True,info = {"label":'请求头'})
    params = db.Column(JSON,nullable=True,info = {"label":'查询参数'})
    payload = db.Column(JSON,nullable=True,info = {"label":'请求体'})
    data  = db.Column(JSON,nullable=True,info = {"label":'请求体'})
    res = db.Column(JSON,nullable=True,info = {"label":'响应体'})
    is_del = db.Column(db.Integer, default=0)
    create_time = db.Column(db.DateTime, default=datetime.datetime.now)
    update_time = db.Column(db.DateTime, default=datetime.datetime.now, onupdate=datetime.datetime.now)

    def __repr__(self):
        return '<ScriptStep %r>' % self.scriptName

class CustomFunc(BaseModel):
    __tablename__ = 'customfunc'
    id = db.Column(db.Integer, primary_key=True)
    FuncName = db.Column(db.String(20))
    FuncBody = db.Column(db.Text)
    description = db.Column(db.String(200),nullable=True)
    create_time = db.Column(db.DateTime, default=datetime.datetime.now)
    update_time = db.Column(db.DateTime, default=datetime.datetime.now, onupdate=datetime.datetime.now)
    is_del = db.Column(db.Integer, default=0)

    def __init__(self, FuncName, FuncBody,description):
        self.FuncName = FuncName
        self.FuncBody = FuncBody
        self.description = description

    def __repr__(self):
        return '<CustomFunc %r>' % self.FuncName